package com.xxx.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.xxx.car.constants.FeedbackStatus;
import com.xxx.car.entity.TbFeedback;
import com.xxx.car.entity.vo.request.ReqFeedBack;
import com.xxx.car.entity.vo.request.ReqFeedBackQuery;
import com.xxx.car.entity.vo.response.RespFeedBack;
import com.xxx.car.mapper.TbFeedbackMapper;
import com.xxx.car.service.FeedbackService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by xuwenjie on 2017/6/22.
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private TbFeedbackMapper tbFeedbackMapper;

    @Override
    public List<RespFeedBack> query(ReqFeedBackQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return tbFeedbackMapper.query(query).stream().map(e -> {
            RespFeedBack respFeedBack = new RespFeedBack();
            BeanUtils.copyProperties(e, respFeedBack);
            return  respFeedBack;
        }).collect(Collectors.toList());
    }

    @Override
    public RespFeedBack getByIdAndUser(Integer id, Integer userId) {
        TbFeedback tbFeedback = tbFeedbackMapper.selectByIdAndUserId(id, userId);
        if (tbFeedback != null) {
            RespFeedBack respFeedBack = new RespFeedBack();
            BeanUtils.copyProperties(tbFeedback, respFeedBack);
            return respFeedBack;
        }
        return null;
    }

    @Override
    public void save(ReqFeedBack reqFeedBack) {
        TbFeedback tbFeedback = new TbFeedback();
        BeanUtils.copyProperties(reqFeedBack, tbFeedback);
        tbFeedback.setCreateTime(new Date());
        tbFeedback.setFeedbackStatus(FeedbackStatus.NOT_DEAL);
        tbFeedbackMapper.insertSelective(tbFeedback);
    }
}
